package com.dyzs.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {
	public FileUtils() {	
		
	}
	
	
	public File getDir(String path){
		java.io.File file = new java.io.File(path);	   
	    if (!file.exists())
	      file.mkdir(); 
	    return file;
	}

	
	public File getFile(String path,String fileName) throws IOException{
		getDir(path);
		File file = new File(path + java.io.File.separator + fileName);		
		if(file.exists()){
			file.delete();
		}
		file.createNewFile();
		return file;
	}
	
	
	public File write2SDFromInput(String path,String fileName,InputStream input){
		File file = null;
		OutputStream output = null;
		try{			
			file = getFile(path,fileName);
			output = new FileOutputStream(file);
			byte buffer [] = new byte[4 * 1024];
			int temp ;
			while((temp = input.read(buffer)) != -1){
				output.write(buffer,0,temp);
			}
			output.flush();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			try{
				output.close();
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		return file;
	}

}